package application.common.ui.controller;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.net.URL;
import java.util.List;
import java.util.ResourceBundle;
import java.util.Timer;
import java.util.TimerTask;
import java.util.regex.Pattern;

import com.jfinal.kit.StrKit;

import application.common.util.FiltUtil;
import application.common.util.HandleMessgeUtil;
import application.common.util.ShiroUtil;
import javafx.application.Platform;
import javafx.event.Event;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;

public class ScreenDialogController implements Initializable {

	@FXML
	private BorderPane borderPane;
	private Stage stage;

	public ScreenDialogController(Stage stage) {
		this.stage = stage;
	}

	private ImageView iv2;

	public void initialize(URL arg0, ResourceBundle arg1) {
		iv2 = new ImageView();
		iv2.setFitWidth(1080);
		iv2.setPreserveRatio(true);
		iv2.setSmooth(true);
		iv2.setCache(false);
		borderPane.setCenter(iv2);
		playAd();
		iv2.setOnMouseClicked(new EventHandler<Event>() {
			@Override
			public void handle(Event event) {
				destroy();
				if (HandleMessgeUtil.getIndexController() != null) {
					HandleMessgeUtil.getIndexController().screenPross();
				}
				stage.close();
			}
		});
	}

	private Timer adTimer;
	
	/**
	 * 宣传栏
	 */
	int picIndex = 0;
	int filesLength =0;
	List<File> files;
	private void playAd() {
		getFiles();
		int period=10;
		if(isInteger(ShiroUtil.getConfig().getIsBiopsy())) {
			period=Integer.parseInt(ShiroUtil.getConfig().getIsBiopsy());
		}
		
		adTimer = new Timer();
		adTimer.schedule(new TimerTask() {
			@Override
			public void run() {
				if (StrKit.equals("0", ShiroUtil.getConfig().getDownPic()) && filesLength>0) {
					if (picIndex >= filesLength) {
						picIndex = 0;
					}
					carousel(files.get(picIndex));
					picIndex++;
				} else {
					getFiles();
				}
			}
		}, 1000, period*1000);
	}
	
	public  boolean isInteger(String str) {  
	    Pattern pattern = Pattern.compile("^[-\\ ]?[\\d]*$");  
	    return pattern.matcher(str).matches();  
	}
	public void getFiles() {
		if (!StrKit.isBlank(ShiroUtil.getConfig().getPicturePath())) {
			files=FiltUtil.getList(ShiroUtil.getConfig().getPicturePath());
			if (files!=null) {
				filesLength = files.size();
				if(filesLength > 0) {
					picIndex = 0;
					carousel(files.get(picIndex));
				}
			}
		}
	}

	public void carousel(File file) {
		Platform.runLater(new Runnable() {
			public void run() {
				try {
					iv2.setImage(new Image(new FileInputStream(file)));
				} catch (FileNotFoundException e) {
				}
			}
		});
	}

	public void destroy() {
		if (adTimer != null) {
			adTimer.cancel();
			adTimer = null;
		}
	}
}
